home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 64-bit_x64 / Japanese / cpsimage.cab / data / xipProcs / colorMap.proc next >
Text File  |  2009-04-23  |  14KB  |  563 lines

  1. // Load scripted support procedures
  2. #load "xipProcs/printLayer.proc";
  3.  
  4. // Dynamically load necessary functions
  5. LoadClasses (filename: "xeng");
  6.  
  7. CLASS XIPCOLORMAP {
  8.   LIST    entries;
  9.   INTEGER nentries;
  10.   INTEGER photometry;
  11.   INTEGER colorseps;
  12.   BOOLEAN debug;
  13.  
  14.   METHOD exec (XIPIMAGE input, STRING method, INTEGER maxcolors)
  15.     RETURNS (INTEGER ncolors)
  16.   {
  17.     STRING   fn = "XIPCOLORMAP.exec";
  18.     XIPCOLOR color;
  19.     XIPIMAGE img;
  20.     LIST     c;
  21.     STRING   str;
  22.     STRING   tmp;
  23.     INTEGER  k, nseps;
  24.     INTEGER  num;
  25.     INTEGER  imgphoto;
  26.     INTEGER  inphoto;
  27.     INTEGER  skipOne = 0;
  28.     INTEGER  cMaxEntries = 256;
  29.  
  30.     if (this.debug)
  31.       print "Executing XIPCOLORMAP.exec";
  32.  
  33.     // Check for full colormap
  34.     if (this.nentries == cMaxEntries) {
  35.       print fn + ": Colormap contains maximum number of entries";
  36.       return;
  37.     }
  38.  
  39.     // Check for invalid requested number of colors
  40.     if ((maxcolors < 1) || (maxcolors > cMaxEntries)) {
  41.       print fn + ": Requested color count must between 1 and 256 inclusive";
  42.       return;
  43.     }
  44.  
  45.     // Adjust requested number of colors if not enough space
  46.     if ((this.nentries + maxcolors) > cMaxEntries)
  47.       maxcolors = cMaxEntries - this.nentries;
  48.  
  49.     // Check photometry
  50.     imgphoto = input.getMember (member: "photometry");
  51.     if (this.photometry == XIP_UNKNOWN_COLOR) {
  52.       this.photometry = imgphoto;
  53.       this.colorseps  = input.getMember (member: "seps");
  54.     }
  55.  
  56.     if ((method == "simple") ||
  57.         (this.photometry == XIP_XRGB_COLOR) ||
  58.         (this.photometry == XIP_SRGB_COLOR) ||
  59.         (this.photometry == XIP_GENRGB_COLOR)) {
  60.       inphoto = this.photometry;
  61.     } else {
  62.       if ((imgphoto == XIP_XRGB_COLOR) ||
  63.           (imgphoto == XIP_SRGB_COLOR) ||
  64.           (imgphoto == XIP_GENRGB_COLOR))
  65.         inphoto = imgphoto;
  66.       else
  67.         inphoto = XIP_SRGB_COLOR;
  68.     }
  69.  
  70.     if (this.debug) {
  71.       print "  imgphoto: " + Photo2Str (photo: imgphoto);
  72.       print "  inphoto:  " + Photo2Str (photo: inphoto);
  73.     }
  74.  
  75.     img = input;
  76.     if (inphoto != imgphoto)
  77.       img = img.cspace (outspace: Photo2Str (photo: inphoto), precise: 1);
  78.  
  79.     // Check interleave
  80.     if (img.getMember (member: "interleave") != XIP_SCANLINE_INTER)
  81.       img = img.interleave (scanline: 1);
  82.  
  83.     // Check bits per pixel
  84.     if (img.getMember (member: "bits") != 8)
  85.       img = img.convert (to: (8));
  86.  
  87.     // Execute mapping operation
  88.     if (method == "simple") {
  89.       str = img.simplemap (entries: maxcolors
  90.               ).toString();
  91.     } else if (method == "diverse") {
  92.       str = img.diversemap (entries: maxcolors
  93.               ).toString();
  94.     } else if (method == "heckbert") {
  95.       str = img.heckbertmap (entries: maxcolors
  96.               ).toString();
  97.     } else if (method == "octree") {
  98.       str = img.octreemap (entries: maxcolors
  99.               ).toString();
  100.     } else {
  101.       print fn + ": Unknown mapping method: " + method;
  102.       return;
  103.     }
  104.  
  105.     nseps = GetImageSeps (photo: inphoto);
  106.     ncolors = 0;
  107.  
  108.     for (tmp=str.getToken (); tmp; tmp=str.getToken ())
  109.       if (tmp == "color:") {
  110.         if (!skipOne) {
  111.           str.getLine ();
  112.           skipOne++;
  113.         } else {
  114.           color            = new (XIPCOLOR);
  115.           color.photometry = inphoto;
  116.  
  117.           c = new (LIST);
  118.           for (k=0; k<nseps; k++)
  119.             c.insert (obj: new (INTEGER, value:0));
  120.  
  121.           tmp = str.getLine ();
  122.           for (k=0; k<nseps; k++) {
  123.             num = tmp.getToken ();
  124.             c[k] = num;
  125.           }
  126.           color.c = c;
  127.  
  128.           color = XIPColorChangespace (incolor: color, photo: this.photometry);
  129.           if (!this.contains (color: color)) {
  130.             this.entries.insert (obj: color, entry: this.nentries);
  131.             this.nentries++;
  132.             ncolors++;
  133.           }
  134.         }
  135.       }
  136.  
  137.     if (this.photometry != inphoto)
  138.       this.sort ();
  139.   }
  140.  
  141.   METHOD apply (XIPIMAGE input)
  142.     RETURNS (XIPIMAGE output)
  143.   {
  144.     XIPIMAGE img = input;
  145.     INTEGER  inphoto;
  146.     INTEGER  origphoto;
  147.     INTEGER  imgphoto;
  148.  
  149.     // Check interleave
  150.     if (img.getMember (member: "interleave") != XIP_SCANLINE_INTER)
  151.       img = img.interleave (scanline: 1);
  152.  
  153.     // Check bits per pixel
  154.     if (img.getMember (member: "bits") != 8)
  155.       img = img.convert (to: (8));
  156.  
  157.     // Get processing photometry
  158.     imgphoto = img.getMember (member: "photometry");
  159.     if ((imgphoto == XIP_XRGB_COLOR) ||
  160.         (imgphoto == XIP_SRGB_COLOR) ||
  161.         (imgphoto == XIP_GENRGB_COLOR)) {
  162.       inphoto = imgphoto;
  163.     } else if ((this.photometry == XIP_XRGB_COLOR) ||
  164.                (this.photometry == XIP_SRGB_COLOR) ||
  165.                (this.photometry == XIP_GENRGB_COLOR)) {
  166.       inphoto = this.photometry;
  167.     } else {
  168.       inphoto = XIP_SRGB_COLOR;
  169.     }
  170.  
  171.     if (this.photometry == inphoto) {
  172.       this.writemap (filename: "cmap");
  173.     } else {
  174.       origphoto = this.photometry;
  175.       this.changespace (photo: inphoto);
  176.       this.sort ();
  177.       this.writemap (filename: "cmap");
  178.       this.changespace (photo: origphoto);
  179.       this.sort ();
  180.     }
  181.  
  182.     if (imgphoto == inphoto)
  183.       output = img.tomap (cmap: "cmap"
  184.                  ).unmap (
  185.                  ).exec (
  186.                  );
  187.     else
  188.       output = img.cspace (outspace: Photo2Str (photo: inphoto), precise: 1
  189.                  ).tomap (cmap: "cmap"
  190.                  ).cspace (outspace: Photo2Str (photo: imgphoto), precise: 1
  191.                  ).exec (
  192.                  );
  193.  
  194.     System (cmd: "rm -f cmap");
  195.   }
  196.  
  197.   METHOD add (XIPCOLOR incolor)
  198.   {
  199.     STRING   fn = "XIPCOLORMAP.add";
  200.     XIPCOLOR color;
  201.  
  202.     // Check input color photometry
  203.     if (incolor.photometry == XIP_UNKNOWN_COLOR) {
  204.       print fn + ": Input color photometry is unknown";
  205.       return;
  206.     }
  207.  
  208.     // Check colormap photometry
  209.     if (this.photometry == XIP_UNKNOWN_COLOR) {
  210.       this.photometry = incolor.photometry;
  211.       this.colorseps  = GetImageSeps (photo: incolor.photometry);
  212.     }
  213.  
  214.     color = XIPColorChangespace (incolor: incolor, photo: this.photometry);
  215.  
  216.     if (!this.contains (color: color)) {
  217.       this.entries.insert (obj: color, entry: this.nentries);
  218.       this.nentries++;
  219.     }
  220.   }
  221.  
  222.   METHOD addList (LIST colors)
  223.   {
  224.     INTEGER i;
  225.  
  226.     for (i=0; i<colors.length (); i++)
  227.       this.add (incolor: colors[i]);
  228.   }
  229.  
  230.   METHOD remove (XIPCOLOR incolor)
  231.   {
  232.     STRING   fn = "XIPCOLORMAP.remove";
  233.     XIPCOLOR color;
  234.     INTEGER  idx;
  235.  
  236.     // Check input color photometry
  237.     if (incolor.photometry == XIP_UNKNOWN_COLOR) {
  238.       print fn + ": Input color photometry is unknown";
  239.       return;
  240.     }
  241.  
  242.     // Check colormap photometry
  243.     if (this.photometry == XIP_UNKNOWN_COLOR) {
  244.       print fn + ": Colormap photometry is unknown";
  245.       return;
  246.     }
  247.  
  248.     color = XIPColorChangespace (incolor: incolor, photo: this.photometry);
  249.  
  250.     idx = this.find (color: color);
  251.     if (idx >= 0) {
  252.       this.entries.remove (entry: idx);
  253.       this.nentries--;
  254.     }
  255.   }
  256.  
  257.   METHOD clear ()
  258.   {
  259.     if (this.entries.length ())
  260.       this.entries = new (LIST);
  261.     this.photometry = XIP_UNKNOWN_COLOR;
  262.     this.colorseps  = 0;
  263.     this.nentries   = 0;
  264.   }
  265.  
  266.   METHOD find (XIPCOLOR color)
  267.     RETURNS (INTEGER entry)
  268.   {
  269.     XIPCOLOR mapcolor;
  270.     INTEGER  i, k, found;
  271.  
  272.     entry = -1;
  273.  
  274.     if (this.nentries == 0) {
  275.       return;
  276.     }
  277.  
  278.     if (color.photometry != this.photometry) {
  279.       return;
  280.     }
  281.  
  282.     for (i=0; i<this.nentries; i++) {
  283.       mapcolor = this.entries[i];
  284.  
  285.       if (XIPColorCompare (lhs: mapcolor, rhs: color) == 0) {
  286.         found = 0;
  287.         break;
  288.       }
  289.     }
  290.  
  291.     if (found) {
  292.       entry = i;
  293.       break;
  294.     }
  295.   }
  296.  
  297.   METHOD contains (XIPCOLOR color)
  298.     RETURNS (INTEGER found)
  299.   {
  300.     if (this.find (color: color) == -1)
  301.       found = 0;
  302.     else
  303.       found = 1;
  304.   }
  305.  
  306.   METHOD sort ()
  307.   {
  308.     XIPCOLOR color;
  309.     LIST     c;
  310.     INTEGER  done, ret;
  311.     INTEGER  l, j, ir, i, k;
  312.  
  313.     for (k=0; k<this.colorseps; k++)
  314.       c.insert (obj: new (INTEGER, value:0));
  315.     color.c          = c;
  316.     color.photometry = this.photometry;
  317.  
  318.     l  = this.nentries / 2;
  319.     ir = this.nentries - 1;
  320.     if (ir <= 0)
  321.       return;
  322.  
  323.     for (done=0; !done;) {
  324.       if (l > 0) {
  325.         l--;
  326.         color.c = this.entries[l].c;
  327.       } else {
  328.         color.c = this.entries[ir].c;
  329.         this.entries[ir].c = this.entries[0].c;
  330.  
  331.         ir--;
  332.         if (ir == 0) {
  333.           this.entries[0].c = color.c;
  334.           done = 1;
  335.           continue;
  336.         }
  337.       }
  338.  
  339.       i = l;
  340.       for (j=l+1; j<=ir; ) {
  341.         if (j < ir)  {
  342.           ret = XIPColorCompare (lhs: this.entries[j], rhs: this.entries[j+1]);
  343.           if (ret < 0)
  344.             j++;
  345.         }
  346.  
  347.         if (XIPColorCompare (lhs: color, rhs: this.entries[j]) < 0) {
  348.           this.entries[i].c = this.entries[j].c;
  349.           i = j;
  350.           j = j + i;
  351.         } else {
  352.           j = ir + 1;
  353.         }
  354.       }
  355.       this.entries[i].c = color.c;
  356.     }
  357.   }
  358.  
  359.   METHOD changespace (INTEGER photo)
  360.   {
  361.     STRING   fn = "XIPCOLORMAP.changespace";
  362.     XIPCOLOR color;
  363.     INTEGER  i, k, nseps;
  364.     STATUS   status;
  365.  
  366.     if ((photo == XIP_UNKNOWN_COLOR) || (photo == XIP_MULTIPART_COLOR)) {
  367.       print fn + ": Cannot change colormap photometry to " +
  368.             Photo2Str (photo: photo) + " photometry";
  369.       return;
  370.     }
  371.  
  372.     if (photo == this.photometry)
  373.       return;
  374.  
  375.     try {
  376.       nseps = GetImageSeps (photo: photo);
  377.  
  378.       for (i=0; i<this.nentries; i++) {
  379.         color = XIPColorChangespace (incolor: this.entries[i], photo: photo);
  380.         this.entries[i] = color;
  381.       }
  382.       this.photometry = photo;
  383.       this.colorseps  = nseps;
  384.     } catch {
  385.       print "WTF";
  386.       print status.message;
  387.       end;
  388.     }
  389.   }
  390.  
  391.   METHOD reduce ()
  392.   {
  393.     XIPCOLOR clr;
  394.     LIST     colorclass, colors, l;
  395.     STRING   colorinfo;
  396.     INTEGER  i, k, classid;
  397.  
  398.     if (this.debug)
  399.       print "Reducing XIPCOLORMAP:";
  400.  
  401.     this.sort ();
  402.  
  403.     for (i=0; i<15; i++)
  404.       colorclass.insert (obj: new (LIST));
  405.  
  406.     if (this.debug)
  407.       print "Colormap Pre-Reduction:";
  408.  
  409.     for (i=0; i<this.nentries; i++) {
  410.       clr     = this.entries[i];
  411.       classid = XIPColorClass (color: clr);
  412.  
  413.       if (this.debug)
  414.         this.printEntryAttributes (entry: i);
  415.  
  416.       colorclass[classid].insert (obj: clr);
  417.     }
  418.  
  419.     for (i=0; i<15; i++) {
  420.       if (colorclass[i].length () > 0) {
  421.         k = colorclass[i].length () / 2;
  422.         l = colorclass[i];
  423.         colors.insert (obj: l[k], entry: colors.length ());
  424.       }
  425.     }
  426.  
  427.     this.nentries = colors.length ();
  428.     this.entries  = colors;
  429.  
  430.     if (this.debug) {
  431.       print "Colormap Post-Reduction:";
  432.       for (i=0; i<this.nentries; i++)
  433.         this.printEntryAttributes (entry: i);
  434.     }
  435.   }
  436.  
  437.   METHOD threshold (XIPCOLOR mincolor, XIPCOLOR maxcolor)
  438.   {
  439.     XIPCOLOR min, max, mapcolor;
  440.     LIST     colors;
  441.     INTEGER  i, k, inrange;
  442.  
  443.     min = XIPColorChangespace (incolor: mincolor, photo: this.photometry);
  444.     max = XIPColorChangespace (incolor: maxcolor, photo: this.photometry);
  445.  
  446.     for (i=0; i<this.nentries; i++) {
  447.       mapcolor = this.entries[i];
  448.  
  449.       inrange = 1;
  450.       for (k=0; k<this.colorseps; k++) {
  451.         if ((mapcolor.c[k] < min.c[k]) || (mapcolor.c[k] > max.c[k])) {
  452.           inrange = 0;
  453.           break;
  454.         }
  455.       }
  456.  
  457.       if (inrange)
  458.         colors.insert (obj: mapcolor, entry: colors.length ());
  459.     }
  460.  
  461.     this.nentries = colors.length ();
  462.     this.entries  = colors;
  463.   }
  464.  
  465.   METHOD readmap (STRING filename, INTEGER photo)
  466.   {
  467.     STRING   fn = "XIPCOLORMAP.readmap";
  468.     XIPCOLOR color;
  469.     LIST     c;
  470.     STRING   str, tmp, val;
  471.     INTEGER  num;
  472.  
  473.     if (!IsFile (filename: filename)) {
  474.       print fn + ": No such file " + filename;
  475.       return;
  476.     }
  477.  
  478.     str = ReadObject (filename: filename, override: 1);
  479.  
  480.     if (photo == XIP_UNKNOWN_COLOR) {
  481.       photo = XIP_SRGB_COLOR;
  482.     } else if (photo == XIP_MULTIPART_COLOR) {
  483.       print fn + ": Colormap cannot have multipart photometry";
  484.       return;
  485.     }
  486.     this.photometry = photo;
  487.  
  488.     this.colorseps = GetImageSeps (photo: photo);
  489.  
  490.     for (tmp=str.getLine (); tmp; tmp=str.getLine ()) {
  491.       c = new (LIST);
  492.  
  493.       for (val=tmp.getToken (); val; val=tmp.getToken ()) {
  494.         num = val;
  495.         c.insert (obj: new (INTEGER, value:num), entry: c.length ());
  496.       }
  497.  
  498.       color = new (XIPCOLOR);
  499.       color.photometry = photo;
  500.       color.c          = c;
  501.  
  502.       if (!this.contains (color: color)) {
  503.         this.entries.insert (obj: color, entry: this.nentries);
  504.         this.nentries++;
  505.       }
  506.     }
  507.   }
  508.  
  509.   METHOD writemap (STRING filename)
  510.   {
  511.     XIPCOLOR color;
  512.     STRING   str;
  513.     INTEGER  i, k;
  514.  
  515.     if (filename) {
  516.       // Remove existing colormap file
  517.       System (cmd: "rm -f " + filename);
  518.  
  519.       // Write colormap values to file
  520.       for (i=0; i<this.nentries; i++) {
  521.         color = this.entries[i];
  522.         str = color.c[0];
  523.         for (k=1; k<this.colorseps; k++)
  524.           str = str + " " + color.c[k];
  525.         str = str + "\n";
  526.         str.Write (filename: filename, mode: "a", value: 1);
  527.       }
  528.     } else {
  529.       // Write colormap values to stderr
  530.       for (i=0; i<this.nentries; i++) {
  531.         color = this.entries[i];
  532.         str = color.c[0];
  533.         for (k=1; k<this.colorseps; k++)
  534.           str = str + " " + color.c[k];
  535.         str = str + "\n";
  536.         str.Write (fd: 2, value: 1);
  537.       }
  538.     }
  539.   }
  540.  
  541.   METHOD printEntryAttributes (INTEGER entry)
  542.   {
  543.     XIPCOLOR clr;
  544.     STRING   clrinfo;
  545.     INTEGER  k;
  546.  
  547.     if ((entry < 0) || (entry >= this.nentries)) {
  548.       print "Map does not contain entry at index " + entry;
  549.       return;
  550.     }
  551.  
  552.     clr     = this.entries[entry];
  553.     clrinfo = "   entry " + entry + ": \n      values = (" + clr.c[0];
  554.  
  555.     for (k=1; k<this.colorseps; k++)
  556.       clrinfo = clrinfo + ", " + clr.c[k];
  557.     clrinfo = clrinfo + ")\n      name   = \""
  558.                       + XIPColorName (color: clr) + "\"";
  559.  
  560.     print clrinfo;
  561.   }
  562. }
  563.